<?php
function addslashes_deep ($value)
{
    if (empty($value)) {
        return $value;
    } else {
        return is_array($value) ? array_map('addslashes_deep', $value) : escape($value);
    }
}
function stripslashes_deep ($value)
{
    if (empty($value)) {
        return $value;
    } else {
        return is_array($value) ? array_map('stripslashes_deep', $value) : unescape($value);
    }
}
function escape ($content)
{
    $content = trim($content);
    $txArray = array("'" => "&#39;" , "\"" => "&#34;");
    $search = array_keys($txArray);
    $replace = array_values($txArray);
    $content = str_replace($search, $replace, $content);
    $content = addslashes($content);
    //static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
    //$txArray = array("|" => "&#124;" , "$" => "&#36;" , "%" => "&#37;" , "@" => "&#64;" , "\'" => "&#39;" , "\"" => "&#34;" , "<" => "&#60;" , ">" => "&#62;" , "(" => "&#40;" , ")" => "&#41;" , "+" => "&#43;" , "," => "&#44;" , "\\" => "&#92;" , "*" => "&#42;");
    //$content = str_replace("&", "[^-_-^]", $content);
    return $content;
}
function unescape ($content)
{
    //$content = str_replace("[^-_-^]", "&", $content);
    //$search = array("&#124;" , "&#36;" , "&#37;" , "&#64;" , "&#39;" , "&#34;" , "&#60;" , "&#62;" , "&#40;" , "&#41;" , "&#43;" , "&#44;" , "&#92;" , "&#42;");
    //$replace = array("|" , "$" , "%" , "@" , "'" , "\"" , "<" , ">" , "(" , ")" , "+" , "," , "\\" , "*");
    //$content = str_replace($search, $replace, $content);
    $content = stripslashes($content);
    return $content;
}
function add_magic_quotes ($array)
{
    foreach ((array) $array as $k => $v) {
        if (is_array($v)) {
            $array[$k] = add_magic_quotes($v);
        } else {
            $array[$k] = addslashes($v);
        }
    }
    return $array;
}
function get_current_url ()
{
    if (! empty($_SERVER["REQUEST_URI"])) {
        $scriptName = $_SERVER["REQUEST_URI"];
        $nowurl = $scriptName;
    } else {
        $scriptName = $_SERVER["PHP_SELF"];
        if (empty($_SERVER["QUERY_STRING"]))
            $nowurl = $scriptName;
        else
            $nowurl = $scriptName . $_SERVER["QUERY_STRING"];
    }
    return $nowurl;
}
function get_random_str ($len)
{
    $chars = array("a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" , "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" , "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9");
    $charsLen = count($chars) - 1;
    shuffle($chars);
    $output = "";
    for ($i = 0; $i < $len; $i ++) {
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
}
function get_random_name ($prefix, $filename)
{
    return uniqid('pic'.$prefix) . mt_rand(10000, 99999) . "." . file_ext($filename);
}
function file_ext ($filename)
{
    $extend = pathinfo($filename);
    $extend = strtolower($extend["extension"]);
    return $extend;
}
function is_email ($user_email)
{
    $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
        if (preg_match($chars, $user_email)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
function replaceHtmlAndJs ($document)
{
    $document = trim($document);
    if (strlen($document) <= 0) {
        return $document;
    }
    $search = array("'<script[^>]*?>.*?</script>'si" , "'<[\/\!]*?[^<>]*?>'si" , "'([\r\n])[\s]+'" , "'&(quot|#34);'i" , "'&(amp|#38);'i" , "'&(lt|#60);'i" , "'&(gt|#62);'i" , "'&(nbsp|#160);'i");
    $replace = array("" , "" , "\\1" , "\"" , "&" , "<" , ">" , " ");
    return @preg_replace($search, $replace, $document);
}
function sub_str ($sourcestr, $start, $length, $append = "...")
{
    $str = '';
    $i = 0;
    $n = 0;
    $str_length = strlen($sourcestr);
    while (($n < $length) and ($i <= $str_length)) {
        $temp_str = substr($sourcestr, $i, 1);
        $ascnum = ord($temp_str);
        if ($ascnum >= 224) {
            $str = $str . substr($sourcestr, $i, 3);
            $i = $i + 3;
            $n ++;
        } elseif ($ascnum >= 192) {
            $str = $str . substr($sourcestr, $i, 2);
            $i = $i + 2;
            $n ++;
        } elseif ($ascnum >= 65 && $ascnum <= 90) {
            $str = $str . substr($sourcestr, $i, 1);
            $i = $i + 1;
            $n ++;
        } else {
            $str = $str . substr($sourcestr, $i, 1);
            $i = $i + 1;
            $n = $n + 0.5;
        }
        if ($n <= $start) {
            $str = '';
            continue;
        }
    }
    if ($str_length > $length) {
        $str = $str . $append;
    }
    return $str;
}
function timer_start ()
{
    global $timestart;
    $mtime = explode(' ', microtime());
    $timestart = $mtime[1] + $mtime[0];
    return true;
}
function timer_stop ($display = 0, $precision = 3)
{
    global $timestart, $timeend;
    $mtime = microtime();
    $mtime = explode(' ', $mtime);
    $timeend = $mtime[1] + $mtime[0];
    $timetotal = $timeend - $timestart;
    $r = number_format($timetotal, $precision);
    if ($display)
        echo $r;
    return $r;
}
function tranTime ($time)
{
    $rtime = date("m-d H:i", $time);
    $htime = date("H:i", $time);
    $time = time() - $time;
    if ($time < 60) {
        $str = '刚刚';
    } elseif ($time < 60 * 60) {
        $min = floor($time / 60);
        $str = $min . '分钟前';
    } elseif ($time < 60 * 60 * 24) {
        $h = floor($time / (60 * 60));
        $str = $h . '小时前 ' . $htime;
    } elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time / (60 * 60 * 24));
        if ($d == 1)
            $str = '昨天 ' . $rtime;
        else
            $str = '前天 ' . $rtime;
    } else {
        $str = $rtime;
    }
    return $str;
}
function utf8_uri_encode ($utf8_string, $length = 0)
{
    $unicode = '';
    $values = array();
    $num_octets = 1;
    $unicode_length = 0;
    $string_length = strlen($utf8_string);
    for ($i = 0; $i < $string_length; $i ++) {
        $value = ord($utf8_string[$i]);
        if ($value < 128) {
            if ($length && ($unicode_length >= $length))
                break;
            $unicode .= chr($value);
            $unicode_length ++;
        } else {
            if (count($values) == 0)
                $num_octets = ($value < 224) ? 2 : 3;
            $values[] = $value;
            if ($length && ($unicode_length + ($num_octets * 3)) > $length)
                break;
            if (count($values) == $num_octets) {
                if ($num_octets == 3) {
                    $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
                    $unicode_length += 9;
                } else {
                    $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
                    $unicode_length += 6;
                }
                $values = array();
                $num_octets = 1;
            }
        }
    }
    return $unicode;
}
function utf8_uri_decode ($str)
{
    $ret = '';
    $len = strlen($str);
    for ($i = 0; $i < $len; $i ++) {
        if ($str[$i] == '%' && $str[$i + 1] == 'u') {
            $val = hexdec(substr($str, $i + 2, 4));
            if ($val < 0x7f) {
                $ret .= chr($val);
            } else 
                if ($val < 0x800) {
                    $ret .= chr(0xc0 | ($val >> 6)) . chr(0x80 | ($val & 0x3f));
                } else {
                    $ret .= chr(0xe0 | ($val >> 12)) . chr(0x80 | (($val >> 6) & 0x3f)) . chr(0x80 | ($val & 0x3f));
                }
            $i += 5;
        } else 
            if ($str[$i] == '%') {
                $ret .= urldecode(substr($str, $i, 3));
                $i += 2;
            } else {
                $ret .= $str[$i];
            }
    }
    return $ret;
}
function generate_password ($pass)
{
    return hash("sha256", $pass . SALT);
}
function uuid ($prefix = '')
{
    $chars = md5(uniqid(mt_rand(), true));
    $uuid = substr($chars, 0, 8) . '-';
    $uuid .= substr($chars, 8, 4) . '-';
    $uuid .= substr($chars, 12, 4) . '-';
    $uuid .= substr($chars, 16, 4) . '-';
    $uuid .= substr($chars, 20, 12);
    return $prefix . $uuid;
}
?>
